#include <iostream>
#include <chrono>  
   
using namespace std;   
using namespace std::chrono;  
 
// 逐个累加求和    
int sumByIteration(int n) {
    int sum = 0;  
    for (int i = 1; i <= n; i++) {
        sum += i;
    } 
    return sum;
} 

// 高斯法求和
int sumByGauss(int n) {
    return n * (n + 1) / 2;
}

int main() {
    int n;
    cout << "请输入一个正整数n：";
    cin >> n;

    // 逐个累加求和
    auto start = high_resolution_clock::now();
    int sum1 = sumByIteration(n);
    auto stop = high_resolution_clock::now();
    auto duration1 = duration_cast<microseconds>(stop - start);

    // 高斯法求和
    start = high_resolution_clock::now();
    int sum2 = sumByGauss(n);
    stop = high_resolution_clock::now();
    auto duration2 = duration_cast<microseconds>(stop - start);

    cout << "逐个累加法求和结果：" << sum1 << endl;
    cout << "逐个累加法求解时间：" << duration1.count() << " 微秒" << endl;
    cout << "高斯法求和结果：" << sum2 << endl;
    cout << "高斯法求解时间：" << duration2.count() << " 微秒" << endl;

    return 0;
}